﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;

namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        void GUILock(bool value)
        {
            this.subscriptionManager.IsEnabled = !value;
            this.lvPlaylists.IsEnabled = !value;
            this.lvVideos.IsEnabled = !value;
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            VLC.StartVLC();
            chkPreventHD.IsChecked = !Properties.Settings.Default.HD;
        }

        private void lvPlaylists_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GUILock(true);
            PlaylistItem pi = e.AddedItems.Count > 0 ? e.AddedItems[0] as PlaylistItem : null;
            if (pi != null)
            {
                lvVideos.ShowPlaylist(pi);
            }
        }

        private void cmdSearch_Click(object sender, RoutedEventArgs e)
        {
            GUILock(true);
            lvVideos.ShowSearch(txtQuery.Text);
        }

        private void lvVideos_DownloadCompleted(object sender, EventArgs e)
        {
            GUILock(false);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            VLC.StopVLC();
        }

        private void lvVideos_VideoDoubleClick(object sender, EventArgs e)
        {
            VLC.Play(sender as Video);
        }

        private void playlistManager_Drop(object sender, DragEventArgs e)
        {
            DataObject dobj = e.Data as DataObject;
            VideoItem vi = dobj.GetData(typeof(VideoItem)) as VideoItem;
            VLC.AddToPlaylist(vi.Video);
        }

        private void subscriptionManager_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GUILock(true);
            string user = e.AddedItems[0].ToString() == "Playlists" ? "default" : e.AddedItems[0].ToString();
            lvPlaylists.SetUser(user);
        }

        private void lvPlaylists_DownloadCompleted(object sender, EventArgs e)
        {
            GUILock(false);
        }

        private void lvPlaylists_PlaylistDoubleClick(object sender, EventArgs e)
        {
            // Ottengo la playlist
            PlaylistItem pi = sender as PlaylistItem;

            // Pulisco la playlist corrente
            VLC.Clear();

            // Inserisco i brani di questa playlist
            foreach (Video video in pi.Videos)
            {
                VLC.AddToPlaylist(video);
            }
        }

        private void chkPreventHD_Checked(object sender, RoutedEventArgs e)
        {
            if (chkPreventHD.IsChecked.HasValue)
            {
                Properties.Settings.Default.HD = !chkPreventHD.IsChecked.Value;
                Properties.Settings.Default.Save();
            }
        }
    }
}
